/************************************************************************

								Heroes IV
					  Copyright 2000, The 3DO Company

	------------------------------------------------------------------
  						       dialog_box.cpp

	$Header: /game/dialog_box.cpp $

	$NoKeywords: $

 ************************************************************************/

// kill the 255+ character identifier warning.  Templates tend to exceed that easily.
#include "precompiled.h"
#pragma warning( disable: 4786 )

#include "dialog_box.h"

#include "dialog_box_bitmaps.h"
#include "dialog_box_cache.h"
#include "mouse_event.h"
#include "pixel_24.h"
#include "platform.h"

// -============================================================
// global variable with dialog box bitmap info
// -============================================================
static t_dialog_box_cache const k_dialog_bitmaps( "dialog.generic" );

t_dialog_box::t_dialog_box( t_window* parent ) 
            : t_window_background( k_dialog_bitmaps.get(), parent )
{
	// get bitmaps
	m_dragging = false;
}

t_dialog_box::t_dialog_box( t_screen_rect const& rect, t_window* parent )
            : t_window_background( k_dialog_bitmaps.get(), rect, parent )
{
	// get bitmaps
	m_dragging = false;
}


// -============================================================
// drag from last point to point
// -============================================================
void t_dialog_box::drag( t_screen_point point )
{
	t_screen_point delta = point - m_drag_start;
	t_screen_rect rect = get_parent()->to_client( get_screen_rect() );
	t_screen_rect parent_rect = get_parent()->get_client_rect();
	
	m_drag_start = point;

	rect += delta;
	if (!parent_rect.contains( rect ))
		return;

	move( rect );
}

// -============================================================
// check if we're dragging the dialog box.
// -============================================================
void t_dialog_box::left_button_down( t_mouse_event const& event )
{
	if (capture_mouse())
	{
		m_dragging = true;
		m_drag_start = event.screen_point;
	}
}

// -============================================================
// check if we're dragging the dialog box.
// -============================================================
void t_dialog_box::left_button_up( t_mouse_event const& event )
{
	if (m_dragging)
	{
		drag( event.screen_point );
		release_mouse();
		m_dragging = false;
	}
}

// -============================================================
// check if we're dragging the dialog box.
// -============================================================
void t_dialog_box::mouse_move( t_mouse_event const& event )
{
	if (!m_dragging)
		return;

	if (!event.left_button)
	{
		drag( event.screen_point );
		release_mouse();
		m_dragging = false;
		return;
	}

	// check event message against current mouse position.
	if ( get_mouse_position( this ) != event.client_point )
		return;

	drag( event.screen_point );
}
